import 'package:flutter/material.dart';
import 'package:gksz/political_Theory/Video/VideoPage.dart';
import 'package:video_player/video_player.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:dio/dio.dart';

Dio dio = new Dio();

class Video extends StatefulWidget {
  Video({Key key}) : super(key: key);
  _VideoState createState() => _VideoState();
}

class _VideoState extends State<Video> {
  bool _loading = false;
  List<String> items = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
  ];
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  void _onRefresh() async {
    // monitor network fetch
    await Future.delayed(Duration(milliseconds: 1500));
    // if failed,use refreshFailed()
    _refreshController.refreshCompleted();
    message();
  }

  void _onLoading() async {
    // monitor network fetch
    await Future.delayed(Duration(milliseconds: 1000));
    if (items.length < 15) {
      items.add((items.length + 1).toString());
    }
    // if failed,use loadFailed(),if no data return,use LoadNodata()

    if (mounted) setState(() {});
    _refreshController.loadComplete();
  }

  @override
  void initState() {
    super.initState();
    message();
  }

  var _res;
  void message() async {
    Response response =
        await dio.get('http://xssw.gdit.edu.cn/sizheng/api/v1/video');
    _res = response.data;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return _res == null
        ? Container(
          width: 100.0,
          height: 100.0,
          decoration: BoxDecoration(
            color: Colors.grey,
            borderRadius: BorderRadius.circular(8.0),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            CircularProgressIndicator(),
            Container(height: 10.0,),
            Text('正在加载...',style: TextStyle(fontSize: 16.0,color: Colors.white),)
          ],
        ),
        )
          
        : Scaffold(
            body: SmartRefresher(
                enablePullDown: true,
                enablePullUp: true,
                header: ClassicHeader(
                  height: 45.0,
                  releaseText: '松开手刷新',
                  refreshingText: '刷新中',
                  completeText: '刷新完成',
                  failedText: '刷新失败',
                  idleText: '下拉刷新',
                ),
                footer: CustomFooter(
                  builder: (BuildContext context, LoadStatus mode) {
                    Widget body;
                    if (mode == LoadStatus.idle) {
                      body = Text("已经到底了");
                    } else if (mode == LoadStatus.loading) {
                      body = CupertinoActivityIndicator();
                    } else if (mode == LoadStatus.failed) {
                      body = Text("加载失败！单击“重试”");
                    } else {
                      body = Text("上拉加载");
                    }
                    return Container(
                      height: 55.0,
                      child: Center(child: body),
                    );
                  },
                ),
                controller: _refreshController,
                onRefresh: _onRefresh,
                onLoading: _onLoading,
                child: ListView.builder(
                  itemCount: _res.length,
                  itemBuilder: (context, index) {
                    return Container(
                      // margin: EdgeInsets.only(left: 20,right: 20,top: 10),
                      child: InkWell(
                          child: Column(
                            children: <Widget>[
                              Stack(
                                children: <Widget>[
                                  Image(
                                    image:
                                        NetworkImage('${_res[index]['album']}'),
                                    height: 200,
                                    width: MediaQuery.of(context).size.width,
                                    fit: BoxFit.cover,
                                  ),
                                  Positioned(
                                    top: 10,
                                    left: 10,
                                    right: 10,
                                    child: Text(
                                      '${_res[index]['title']}',
                                      style: TextStyle(
                                          color: Colors.white,
                                          fontSize: 16,
                                          fontWeight: FontWeight.w600),
                                      maxLines: 2,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ),
                                ],
                              ),
                              Row(
                                children: <Widget>[
                                  Container(
                                    padding: EdgeInsets.all(10),
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      '${_res[index]['from']}',
                                      style: TextStyle(
                                          fontSize: 14.0, color: Colors.black),
                                    ),
                                  ),
                                  Container(
                                    padding: EdgeInsets.all(10),
                                    alignment: Alignment.centerLeft,
                                    child: Text(
                                      '${_res[index]['createdAt'].substring(0, 10)}',
                                      style: TextStyle(
                                          fontSize: 14.0, color: Colors.black),
                                    ),
                                  ),
                                ],
                              ),
                              Container(
                                margin: EdgeInsets.only(bottom: 5.0),
                                width: MediaQuery.of(context).size.width,
                                height: 1,
                                decoration:
                                    BoxDecoration(color: Colors.grey[400]),
                              )
                            ],
                          ),
                          onTap: () {
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) =>
                                    VideoPage(msg: _res[index])));
                          }),
                    );
                  },
                )),
          );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
